
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>组合模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../../behavioralmodel/" />
    
    
    <link rel="prev" href="../flyweightmode/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../../../../">
            
                <a href="../../../../../../">
            
                    
                    说明
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../../../../">
            
                <a href="../../../../../">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" >
            
                <span>
            
                    
                    创建型模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1.1" >
            
                <span>
            
                    
                    简单工厂模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.2" >
            
                <span>
            
                    
                    工厂方法模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.3" data-path="../../createdmodel/abstractfactorymode/">
            
                <a href="../../createdmodel/abstractfactorymode/">
            
                    
                    抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.4" data-path="../../createdmodel/bulidermode/">
            
                <a href="../../createdmodel/bulidermode/">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.5" data-path="../../createdmodel/prototypemode/">
            
                <a href="../../createdmodel/prototypemode/">
            
                    
                    原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.6" data-path="../../createdmodel/singletonmode/">
            
                <a href="../../createdmodel/singletonmode/">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../">
            
                <a href="../">
            
                    
                    结构型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.2.1" data-path="../proxymode/">
            
                <a href="../proxymode/">
            
                    
                    代理模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.2" data-path="../adaptermode/">
            
                <a href="../adaptermode/">
            
                    
                    适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.3" data-path="../bridgemode/">
            
                <a href="../bridgemode/">
            
                    
                    桥接模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.4" data-path="../decoratormode/">
            
                <a href="../decoratormode/">
            
                    
                    装饰模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.5" data-path="../facademode/">
            
                <a href="../facademode/">
            
                    
                    外观模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.6" data-path="../flyweightmode/">
            
                <a href="../flyweightmode/">
            
                    
                    享元模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.2.7" data-path="./">
            
                <a href="./">
            
                    
                    组合模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../behavioralmodel/">
            
                <a href="../../behavioralmodel/">
            
                    
                    行为型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.3.1" data-path="../../behavioralmodel/templatemethodmode/">
            
                <a href="../../behavioralmodel/templatemethodmode/">
            
                    
                    模板方法模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.2" data-path="../../behavioralmodel/strategymode/">
            
                <a href="../../behavioralmodel/strategymode/">
            
                    
                    策略模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.3" data-path="../../behavioralmodel/commandmode/">
            
                <a href="../../behavioralmodel/commandmode/">
            
                    
                    命令模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.4" data-path="../../behavioralmodel/chainofresponsibility/">
            
                <a href="../../behavioralmodel/chainofresponsibility/">
            
                    
                    责任链模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.5" data-path="../../behavioralmodel/statemode/">
            
                <a href="../../behavioralmodel/statemode/">
            
                    
                    状态模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.6" data-path="../../behavioralmodel/observermode/">
            
                <a href="../../behavioralmodel/observermode/">
            
                    
                    观察者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.7" data-path="../../behavioralmodel/mediatormode/">
            
                <a href="../../behavioralmodel/mediatormode/">
            
                    
                    中介者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.8" data-path="../../behavioralmodel/iteratormode/">
            
                <a href="../../behavioralmodel/iteratormode/">
            
                    
                    迭代器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.9" data-path="../../behavioralmodel/visitormode/">
            
                <a href="../../behavioralmodel/visitormode/">
            
                    
                    访问者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.10" data-path="../../behavioralmodel/mementomode/">
            
                <a href="../../behavioralmodel/mementomode/">
            
                    
                    备忘录模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.11" data-path="../../behavioralmodel/interpretermode/">
            
                <a href="../../behavioralmodel/interpretermode/">
            
                    
                    解释器模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../../../../../Nacos/">
            
                <a href="../../../../../../Nacos/">
            
                    
                    Nacos
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../../../../../Nacos/Nacos-Introduction.html">
            
                <a href="../../../../../../Nacos/Nacos-Introduction.html">
            
                    
                    Nacos介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../../../../../Nacos/Nacos-Registration.html">
            
                <a href="../../../../../../Nacos/Nacos-Registration.html">
            
                    
                    Nacos注册中心
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                <a href="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                    
                    Nacos+OpenFeign
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../../../../../Sortings/">
            
                <a href="../../../../../../Sortings/">
            
                    
                    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                    
                    希尔排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" >
            
                <span>
            
                    
                    选择排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" >
            
                <span>
            
                    
                    堆排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" >
            
                <span>
            
                    
                    希尔排序
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../../../.." >组合模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x7EC4;&#x5408;&#x6A21;&#x5F0F;">&#x7EC4;&#x5408;&#x6A21;&#x5F0F;</h3>
<p>&#x53C8;&#x53EB;&#x505A;&#x201C;&#x90E8;&#x5206;-&#x6574;&#x4F53;&#x6A21;&#x5F0F;&#x201D;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x79CD;&#x5C06;&#x5BF9;&#x8C61;&#x7EC4;&#x5408;&#x6210;&#x6811;&#x72B6;&#x5C42;&#x6B21;&#x7ED3;&#x6784;&#x7684;&#x6A21;&#x5F0F;&#xFF0C;&#x7528;&#x6765;&#x8868;&#x793A;&#x201C;&#x90E8;&#x5206;-&#x6574;&#x4F53;&#x201D;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x4F7F;&#x7528;&#x6237;&#x5BF9;&#x5355;&#x4E2A;&#x5BF9;&#x8C61;&#x548C;&#x7EC4;&#x5408;&#x5BF9;&#x8C61;&#x5177;&#x6709;&#x4E00;&#x81F4;&#x7684;&#x8BBF;&#x95EE;&#x6027;</p>
<p>eg&#xFF1A;&#x4E00;&#x4E2A;&#x516C;&#x53F8;&#x7684;&#x7684;&#x7EC4;&#x7EC7;&#x5EFA;&#x6784;&#x5C31;&#x662F;&#x5178;&#x578B;&#x7684;&#x7EC4;&#x5408;&#x6A21;&#x5F0F;&#xFF0C;&#x5BF9;&#x5916;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x516C;&#x53F8;&#xFF0C;&#x5185;&#x90E8;&#x5176;&#x5B9E;&#x662F;&#x7531;&#x591A;&#x4E2A;&#x5B50;&#x516C;&#x53F8;&#x5BF9;&#x8C61;&#x7EC4;&#x5408;&#x800C;&#x6210;</p>
<p>&#x518D;&#x6BD4;&#x5982; &#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x5939;&#x4E0E;&#x6587;&#x4EF6;&#x7684;&#x5173;&#x7CFB;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x7EC4;&#x5408;&#x6A21;&#x5F0F;</p>
<h3 id="&#x4F18;&#x7F3A;&#x70B9;">&#x4F18;&#x7F3A;&#x70B9;</h3>
<p>&#x4F18;&#x70B9;</p>
<ul>
<li>&#x53EF;&#x4EE5;&#x5B9E;&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;&#x4E00;&#x81F4;&#x6027;&#x7684;&#x5904;&#x7406;&#x5355;&#x4E2A;&#x5BF9;&#x8C61;&#x548C;&#x7EC4;&#x5408;&#x5BF9;&#x8C61;&#xFF0C;&#x65E0;&#x9700;&#x5173;&#x5FC3;&#x4ED6;&#x662F;&#x4EC0;&#x4E48;&#x5BF9;&#x8C61;</li>
<li>&#x7EC4;&#x5408;&#x4F53;&#x5185;&#x52A0;&#x5165;&#x65B0;&#x5BF9;&#x8C61;&#x76F8;&#x5BF9;&#x5BB9;&#x6613;&#xFF0C;&#x5E76;&#x4E14;&#x5BA2;&#x6237;&#x7AEF;&#x4E0D;&#x4F1A;&#x53D7;&#x5230;&#x5F71;&#x54CD;</li>
</ul>
<p>&#x7F3A;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x8BBE;&#x8BA1;&#x590D;&#x6742;&#x5EA6;&#x589E;&#x52A0;</li>
</ul>
<h3 id="&#x89D2;&#x8272;&#x548C;&#x5B9E;&#x73B0;">&#x89D2;&#x8272;&#x548C;&#x5B9E;&#x73B0;</h3>
<ul>
<li><p>&#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x4E3A;&#x6811;&#x53F6;&#x3001;&#x6811;&#x679D;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#x751F;&#x547D;&#x516C;&#x5171;&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x5B9E;&#x73B0;&#x5176;&#x9ED8;&#x8BA4;&#x884C;&#x4E3A;&#x3002;</p>
</li>
<li><p>&#x6811;&#x53F6;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x662F;&#x7EC4;&#x5408;&#x4E2D;&#x7684;&#x53F6;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x4ED6;&#x6CA1;&#x6709;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x7528;&#x4E8E;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#x4E2D;&#x7684;&#x58F0;&#x660E;&#x7684;&#x516C;&#x5171;&#x63A5;&#x53E3;</p>
</li>
<li><p>&#x6811;&#x679D;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x662F;&#x7EC4;&#x5408;&#x4E2D;&#x7684;&#x5206;&#x652F;&#x8282;&#x70B9;&#x5BF9;&#x8C61;&#xFF0C;&#x4ED6;&#x6709;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x5B83;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x6784;&#x5EFA;&#x89D2;&#x8272;&#x4E2D;&#x58F0;&#x660E;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5176;&#x4F5C;&#x7528;&#x662F;&#x5B58;&#x50A8;&#x548C;&#x7BA1;&#x7406;&#x5B50;&#x90E8;&#x4EF6;&#xFF0C;&#x901A;&#x5E38;&#x5305;&#x542B;add&#x3001;remove&#x3001;getchild&#x65B9;&#x6CD5;</p>
</li>
<li><p>&#x5BA2;&#x6237;&#x89D2;&#x8272;&#xFF1A;&#x901A;&#x8FC7;&#x62BD;&#x8C61;&#x6784;&#x5EFA;&#x89D2;&#x8272;&#x8BBF;&#x95EE;&#x63A7;&#x5236;&#x5BF9;&#x8C61;</p>
</li>
</ul>
<p>&#x7EC4;&#x5408;&#x6A21;&#x5F0F;&#x5206;&#x4E3A;<code>&#x900F;&#x660E;&#x5F0F;</code>&#x7EC4;&#x5408;&#x6A21;&#x5F0F;&#x548C;<code>&#x5B89;&#x5168;&#x5F0F;</code>&#x7EC4;&#x5408;&#x6A21;&#x5F0F;</p>
<p>&#x900F;&#x660E;&#x5F0F;&#xFF1A;&#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#x58F0;&#x660E;&#x4E86;&#x6240;&#x6709;&#x5B50;&#x7C7B;&#x4E2D;&#x7684;&#x5168;&#x90E8;&#x65B9;&#x6CD5;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x65E0;&#x9700;&#x533A;&#x522B;&#x6811;&#x53F6;&#x5BF9;&#x8C61;&#x548C;&#x6811;&#x679D;&#x5BF9;&#x8C61;&#xFF0C;&#x5BF9;&#x5BA2;&#x6237;&#x7AEF;&#x6765;&#x8BF4;&#x662F;&#x900F;&#x660E;&#x7684;&#xFF0C;&#x5BFC;&#x81F4;&#x6811;&#x53F6;&#x5BF9;&#x8C61;&#x672C;&#x6765;&#x662F;&#x6CA1;&#x6709;add&#x3001;remove&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x7684;&#x5374;&#x8981;&#x53BB;&#x5B9E;&#x73B0;&#x4ED6;&#x4EEC;&#x5462;&#xFF0C;&#x5B58;&#x5728;&#x5B89;&#x5168;&#x6027;&#x95EE;&#x9898;</p>
<p><img src="http://c.biancheng.net/uploads/allimg/181115/3-1Q1151G62L17.gif" alt=""></p>
<p>&#x5B89;&#x5168;&#x5F0F;&#xFF1A;&#x5C06;&#x7BA1;&#x7406;&#x5B50;&#x6784;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#x79FB;&#x5230;&#x6811;&#x679D;&#x6784;&#x4EF6;&#x4E2D;&#xFF0C;&#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x548C;&#x6811;&#x53F6;&#x6784;&#x4EF6;&#x6CA1;&#x6709;&#x5BF9;&#x5B50;&#x5BF9;&#x8C61;&#x7684;&#x7BA1;&#x7406;&#x65B9;&#x6CD5;&#xFF0C;&#x5982;&#x6B64;&#x907F;&#x514D;&#x4E86;&#x900F;&#x660E;&#x5F0F;&#x7684;&#x5B89;&#x5168;&#x6027;&#x95EE;&#x9898;&#xFF0C;&#x4F46;&#x7531;&#x4E8E;&#x53F6;&#x5B50;&#x548C;&#x5206;&#x652F;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5728;&#x8C03;&#x7528;&#x65F6;&#x8981;&#x6307;&#x4EE3;&#x6811;&#x53F6;&#x5BF9;&#x8C61;&#x548C;&#x6811;&#x679D;&#x5BF9;&#x8C61;&#x7684;&#x5B58;&#x5728;&#xFF0C;&#x6240;&#x4EE5;&#x5931;&#x53BB;&#x4E86;&#x900F;&#x660E;&#x6027;</p>
<p><img src="http://c.biancheng.net/uploads/allimg/181115/3-1Q1151GF5221.gif" alt=""></p>
<pre><code class="lang-java">

<span class="hljs-comment">/**
 * &#x63CF;&#x8FF0;:
 *
 * &#x8BBF;&#x95EE;&#x6811;&#x5F62;&#x96C6;&#x5408;
 *              c0
 *      leaf1       c1
 *            leaf2   leaf3
 * &#x900F;&#x660E;&#x5F0F;&#x7EC4;&#x5408;&#x6A21;&#x5F0F;
 *
 * <span class="hljs-doctag">@author</span> lars
 * <span class="hljs-doctag">@date</span> 2019/7/8 14:32
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CompositeModeTest1</span> </span>{

    <span class="hljs-comment">/***
     * &#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x89D2;&#x8272;
     */</span>
    <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Compoent</span> </span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(Compoent compoent)</span></span>;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(Compoent compoent)</span></span>;

        <span class="hljs-function"><span class="hljs-keyword">public</span> Compoent <span class="hljs-title">getChild</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span></span>;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">operation</span><span class="hljs-params">()</span></span>;
    }

    <span class="hljs-comment">/**
     * &#x6811;&#x53F6;&#x6784;&#x4EF6;&#x89D2;&#x8272;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Leaf</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Compoent</span> </span>{


        <span class="hljs-keyword">private</span> String name;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Leaf</span><span class="hljs-params">(String name)</span> </span>{
            <span class="hljs-keyword">this</span>.name = name;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(Compoent compoent)</span> </span>{

        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(Compoent compoent)</span> </span>{

        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> Compoent <span class="hljs-title">getChild</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">operation</span><span class="hljs-params">()</span> </span>{
            System.out.println(<span class="hljs-string">&quot;&#x6811;&#x53F6; &quot;</span> + name + <span class="hljs-string">&quot; &#x88AB;&#x8BBF;&#x95EE;&quot;</span>);
        }
    }


    <span class="hljs-comment">/**
     * &#x6811;&#x679D;&#x6784;&#x4EF6;&#x89D2;&#x8272;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Composite</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Compoent</span></span>{

        <span class="hljs-keyword">private</span> List&lt;Compoent&gt; compoents = <span class="hljs-keyword">new</span> ArrayList&lt;Compoent&gt;();

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(Compoent compoent)</span> </span>{
            compoents.add(compoent);
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(Compoent compoent)</span> </span>{
            compoents.remove(compoent);
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> Compoent <span class="hljs-title">getChild</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{
            <span class="hljs-keyword">return</span> compoents.get(i);
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">operation</span><span class="hljs-params">()</span> </span>{
            compoents.forEach(Compoent::operation);
        }
    }

    <span class="hljs-comment">/**
     * &#x5BA2;&#x6237;&#x7AEF;&#x89D2;&#x8272;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span></span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Client</span><span class="hljs-params">()</span> </span>{

            Compoent c0 = <span class="hljs-keyword">new</span> Composite();
            Compoent c1 = <span class="hljs-keyword">new</span> Composite();
            Compoent leaf1 = <span class="hljs-keyword">new</span> Leaf(<span class="hljs-string">&quot;1&quot;</span>);
            Compoent leaf2 = <span class="hljs-keyword">new</span> Leaf(<span class="hljs-string">&quot;2&quot;</span>);
            Compoent leaf3 = <span class="hljs-keyword">new</span> Leaf(<span class="hljs-string">&quot;3&quot;</span>);
            c0.add(leaf1);
            c0.add(c1);
            c1.add(leaf2);
            c1.add(leaf3);

            c0.operation();

            c1.operation();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">new</span> Client();
    }
}
</code></pre>
<h3 id="&#x6A21;&#x62DF;&#x573A;&#x666F;">&#x6A21;&#x62DF;&#x573A;&#x666F;</h3>
<p><img src="https://raw.githubusercontent.com/larscheng/myImg/master/blogImg/DesignPatterns/20190708162505.png" alt=""></p>
<p>&#x7ECF;&#x5178;&#x6A21;&#x62DF;&#x573A;&#x666F;&#xFF0C;&#x6587;&#x4EF6;&#x6587;&#x4EF6;&#x5939;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> structuredmodel.compositemode;

<span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-keyword">import</span> java.util.List;

<span class="hljs-comment">/**
 * &#x63CF;&#x8FF0;:&#x6587;&#x4EF6;&#x6587;&#x4EF6;&#x5939;---&#x7EC4;&#x5408;&#x6A21;&#x5F0F;
 *
 * <span class="hljs-doctag">@author</span> lars
 * <span class="hljs-doctag">@date</span> 2019/7/8 15:40
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileDemoTest</span> </span>{

    <span class="hljs-comment">/**
     * &#x62BD;&#x8C61;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x6587;&#x4EF6;
     */</span>
    <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">File</span></span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span></span>;
    }

    <span class="hljs-comment">/**
     * &#x6811;&#x53F6;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x6587;&#x672C;&#x6587;&#x4EF6;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">textFile</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">File</span></span>{

        <span class="hljs-keyword">private</span> String name;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">textFile</span><span class="hljs-params">(String name)</span> </span>{
            <span class="hljs-keyword">this</span>.name = name;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span> </span>{
            System.out.println(<span class="hljs-string">&quot;txt&#x6587;&#x4EF6;&#xFF1A;&quot;</span>+name);
        }
    }

    <span class="hljs-comment">/***
     * &#x6811;&#x53F6;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x56FE;&#x7247;&#x6587;&#x4EF6;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">imgFile</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">File</span></span>{

        <span class="hljs-keyword">private</span> String name;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">imgFile</span><span class="hljs-params">(String name)</span> </span>{
            <span class="hljs-keyword">this</span>.name = name;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span> </span>{
            System.out.println(<span class="hljs-string">&quot;&#x56FE;&#x7247;&#x6587;&#x4EF6;&#xFF1A;&quot;</span>+name);
        }
    }

    <span class="hljs-comment">/**
     * &#x6811;&#x679D;&#x6784;&#x4EF6;&#x89D2;&#x8272;&#xFF1A;&#x6587;&#x4EF6;&#x5939;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Folder</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">File</span></span>{

        <span class="hljs-keyword">private</span> String name;
        <span class="hljs-keyword">private</span> List&lt;File&gt; files = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Folder</span><span class="hljs-params">(String name)</span> </span>{
            <span class="hljs-keyword">this</span>.name = name;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(File file)</span> </span>{
            files.add(file);
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">del</span><span class="hljs-params">(File file)</span> </span>{
            files.remove(file);
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span> </span>{
            System.out.println(<span class="hljs-string">&quot;&#x5F53;&#x524D;&#x6587;&#x4EF6;&#x5939;&#x76EE;&#x5F55;: &quot;</span>+name);
            files.forEach(File::display);
        }
    }

    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span></span>{

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Client</span><span class="hljs-params">()</span> </span>{
            Folder folder1 = <span class="hljs-keyword">new</span> Folder(<span class="hljs-string">&quot;/root&quot;</span>);
            Folder folder2 = <span class="hljs-keyword">new</span> Folder(<span class="hljs-string">&quot;/root/home&quot;</span>);

            textFile textFile1 = <span class="hljs-keyword">new</span> textFile(<span class="hljs-string">&quot;demo.txt&quot;</span>);
            textFile textFile2 = <span class="hljs-keyword">new</span> textFile(<span class="hljs-string">&quot;example.txt&quot;</span>);
            imgFile imgFile1 = <span class="hljs-keyword">new</span> imgFile(<span class="hljs-string">&quot;demo.jpg&quot;</span>);
            imgFile imgFile2 = <span class="hljs-keyword">new</span> imgFile(<span class="hljs-string">&quot;example.jpg&quot;</span>);

            folder1.add(textFile1);
            folder1.add(folder2);

            folder2.add(textFile2);
            folder2.add(imgFile1);
            folder2.add(imgFile2);

            folder1.display();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">new</span> Client();
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../flyweightmode/" class="navigation navigation-prev " aria-label="Previous page: 享元模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../../behavioralmodel/" class="navigation navigation-next " aria-label="Next page: 行为型模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"组合模式","level":"2.1.2.7","depth":3,"next":{"title":"行为型模式","level":"2.1.3","depth":2,"path":"Design-Patterns/src/main/java/behavioralmodel/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/README.md","articles":[{"title":"模板方法模式","level":"2.1.3.1","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/templatemethodmode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/templatemethodmode/README.md","articles":[]},{"title":"策略模式","level":"2.1.3.2","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/strategymode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/strategymode/README.md","articles":[]},{"title":"命令模式","level":"2.1.3.3","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/commandmode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/commandmode/README.md","articles":[]},{"title":"责任链模式","level":"2.1.3.4","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/chainofresponsibility/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/chainofresponsibility/README.md","articles":[]},{"title":"状态模式","level":"2.1.3.5","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/statemode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/statemode/README.md","articles":[]},{"title":"观察者模式","level":"2.1.3.6","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/observermode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/observermode/README.md","articles":[]},{"title":"中介者模式","level":"2.1.3.7","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/mediatormode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/mediatormode/README.md","articles":[]},{"title":"迭代器模式","level":"2.1.3.8","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/iteratormode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/iteratormode/README.md","articles":[]},{"title":"访问者模式","level":"2.1.3.9","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/visitormode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/visitormode/README.md","articles":[]},{"title":"备忘录模式","level":"2.1.3.10","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/mementomode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/mementomode/README.md","articles":[]},{"title":"解释器模式","level":"2.1.3.11","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/interpretermode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/interpretermode/README.md","articles":[]}]},"previous":{"title":"享元模式","level":"2.1.2.6","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/flyweightmode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/flyweightmode/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["expandable-chapters","livereload"],"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Design-Patterns/src/main/java/structuredmodel/compositemode/README.md","mtime":"2019-07-08T11:05:14.984Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-09T07:20:35.937Z"},"basePath":"../../../../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../../../../gitbook/gitbook.js"></script>
    <script src="../../../../../../gitbook/theme.js"></script>
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

